<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <!--<el-input v-model="listQuery.id" clearable class="filter-item" style="width: 200px;" placeholder="请输入id"/>-->
      <el-input size="mini" v-model="listQuery.categoryName" clearable class="filter-item" style="width: 200px;"
        placeholder="行业类别名称" />
      <el-select size="mini" v-model="listQuery.categoryType" clearable class="filter-item" style="width: 200px;"
        placeholder="选择类别类型">
        <el-option value="1" label="行业大类" />
        <el-option value="2" label="行业小类" />
        <el-option value="3" label="具体行业" />
      </el-select>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找
      </el-button>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-edit" @click="createIndustry()">添加
      </el-button>
      <!-- <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button> -->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" border size="mini" element-loading-text="正在查询中。。。"
      highlight-current-row>
      <el-table-column align="left" label="行业名称" prop="categoryName" width="300px">
        <template slot-scope="scope">
          <span style="color:#1679FB;font-size: 14px;font-weight: bold;">
            {{scope.row.categoryName}}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="类型" prop="categoryType" width="120px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.categoryType===1" type="success">行业大类</el-tag>
          <el-tag v-if="scope.row.categoryType===2" type="primary">行业小类</el-tag>
          <el-tag v-if="scope.row.categoryType===3" type="info">具体行业</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序" prop="categorySort" width="100px" />
      <el-table-column align="center" label="企业人数" prop="companyNumber" width="100px" />
      <el-table-column align="center" label="培训人数" prop="studentNumber" width="100px" />
      <!--<el-table-column align="center" label="状态" prop="deleteMark">-->
      <!--<template slot-scope="scope">-->
      <!--<el-tag v-if="scope.row.deleteMark===0" type="success">正常</el-tag>-->
      <!--<el-tag v-if="scope.row.deleteMark===1" type="info">删除</el-tag>-->
      <!--</template>-->
      <!--</el-table-column>-->

      <!--<el-table-column align="center" label="创建时间" prop="createDate"/>-->
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="updateIndustry(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px"
        style="width: 400px; margin-left:50px;">
        <el-form-item label="行业大类名称" prop="adLocationType">
          <el-input v-model="dataForm.adLocationType" type="number" />
        </el-form-item>
        <el-form-item label="广告介绍" prop="adIntroduce">
          <el-input v-model="dataForm.adIntroduce" />
        </el-form-item>
        <el-form-item label="跳转链接" prop="linkUrl">
          <el-input v-model="dataForm.linkUrl" />
        </el-form-item>
        <el-form-item label="排序" prop="adSort">
          <el-input v-model="dataForm.adSort" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取消</el-button>
        <el-button size="mini" v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button size="mini" v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 145px;
    height: 145px;
    display: block;
  }
</style>

<script>
  import {
    listIndustry,
    updateIndustry,
    createIndustry,
    deleteIndustry
  } from '@/api/industryCategory'
  import {
    uploadPath
  } from '@/api/storage'
  import {
    getToken
  } from '@/utils/auth'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  export default {
    name: 'IndustryCategory',
    components: {
      Pagination
    },
    data() {
      return {
        uploadPath,
        updateParam: undefined,
        list: undefined,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          id: undefined,
          categoryName: undefined,
          categoryType: undefined,
          sort: 'category_sort',
          order: 'asc'
        },
        dataForm: {
          id: undefined,
          pid: undefined,
          categoryName: undefined,
          categoryType: undefined,
          categorySort: undefined,
          deleteMark: undefined,
          createDate: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
          name: [{
            required: true,
            message: '商家名称不能为空',
            trigger: 'blur'
          }]
        },
        downloadLoading: false
      }
    },
    computed: {
      headers() {
        return {
          'X-Litemall-Admin-Token': getToken()
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        listIndustry(this.listQuery)
          .then(response => {
            this.list = response.data.data.items
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch(() => {
            this.list = []
            this.total = 0
            this.listLoading = false
          })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetForm() {
        this.dataForm = {
          id: undefined,
          pid: undefined,
          categoryName: undefined,
          categoryType: undefined,
          categorySort: undefined,
          deleteMark: undefined,
          createDate: undefined
        }
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      uploadPicUrl: function(response) {
        this.dataForm.picUrl = response.data.url
      },
      createData() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            createIndustry(this.dataForm)
              .then(response => {
                this.list.unshift(response.data.data)
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '创建成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
              })
          }
        })
      },
      handleStatus(parm, status) {
        this.updateParam = Object.assign({}, parm)
        this.updateParam.adStatus = status
        let content = ''
        if (status === 0) {
          content = '是否下架广告？'
        } else {
          content = '是否上架广告！！'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (status === 0) {
            updateIndustry(this.updateParam)
              .then(() => {
                this.$notify.success({
                  title: '成功',
                  message: '广告已下架'
                })
                // 刷新列表数据
                this.getList()
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
                // 刷新列表数据
                this.getList()
              })
          } else {
            updateIndustry(this.updateParam)
              .then(() => {
                this.$notify.success({
                  title: '成功',
                  message: '广告已上架'
                })
                // 刷新列表数据
                this.getList()
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
                // 刷新列表数据
                this.getList()
              })
          }
        }).catch(() => {
          this.getList()
        })
      },
      handleUpdate(row) {
        this.dataForm = Object.assign({}, row)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true

        // queryIndustryCategory(this.dataForm)
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            updateIndustry(this.dataForm)
              .then(() => {
                for (const v of this.list) {
                  if (v.id === this.dataForm.id) {
                    const index = this.list.indexOf(v)
                    this.list.splice(index, 1, this.dataForm)
                    break
                  }
                }
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '更新成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.data.message
                })
              })
          }
        })
      },
      handleDelete(row) {
        deleteIndustry(row)
          .then(response => {
            this.getList()
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.message
            })
          })
      },
      handleDownload() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = [
            '商家ID',
            '商家名称',
            '介绍',
            '低价',
            '商家图片'
          ]
          const filterVal = ['id', 'name', 'desc', 'floorPrice', 'picUrl']
          excel.export_json_to_excel2(
            tHeader,
            this.list,
            filterVal,
            '商家信息'
          )
          this.downloadLoading = false
        })
      },
      updateIndustry(row) {
        this.$router.push({
          path: '/industryManage/industryCreateOrUpdate',
          query: {
            id: row.id,
            pid: row.pid,
            type: row.categoryType
          }
        })
      },
      createIndustry() {
        this.$router.push({
          path: '/industryManage/industryCreateOrUpdate'
        })
      }

    }
  }
</script>
